<?php
class App_Zend_Controller_Plugin extends Zend_Controller_Plugin_Abstract 
{
	public function routeStartup(Zend_Controller_Request_Abstract $request)
	{
		$frontController = Zend_Controller_Front::getInstance();
        $frontController->setDefaultModule('admin')
                        ->setControllerDirectory(array('admin'=>_APP_DIR_ . '/admin/controllers')); 
		     
	}
	
    public function preDispatch(Zend_Controller_Request_Abstract $request)
    {
        $moduleName = $request->getModuleName();
        $controllerName = $request->getControllerName();
        $actionName = $request->getActionName();
        
        //权限控制
        $resourceName = '';        
        $resourceName .= $controllerName;
        
        Zend_Session::start();   
        $userNameSpace = new Zend_Session_Namespace('User');
        $user = $userNameSpace->user;
        
        if (!$user) {
            $roleName = 'Guest';  
        } else {
            if ($user->is_admin == 1) {
            	$roleName = 'Admin';
            } else {
            	$roleName = 'User';
            }
        }
    	$rights = new App_Rights();
    	
    	$rights->isAllowed($roleName, $resourceName, $actionName) || $this->denyAccess();
    }
    
    public function denyAccess()
    {
        $this->_request->setModuleName($this->_request->getModuleName());
        $this->_request->setControllerName('error');
        $this->_request->setActionName('denied');
    }
}
